// 使用时注意类型转换
var dateTime = {
    year: '2014',
    month: '05',
    day: '13',
    hour: '0',
    week: '2'
}

var selectedSites = []

var genData = ''

var TimeData = ''

var mapData = ''

var voiData = ''

var hier_res = ''

var levels = ''

var selectVoi = ''
// 阈值
var threshold = 50;

// 时序分析类型
var timeAnlysisType = 'day'

// 页面加载
function load() {
    drawTimeAnlysisChart();
    drawNavigatorChart();
    getData();
    getMapData();
    voronoiClear();
}

// 获取导航图数据
function getData() {
    $.ajax({
        url: "http://localhost:5000/getData",
        data: dateTime,
        type: "GET",
        success: function (data) {
            genData = data
            drawHourarc();
            drawDayarc();
            drawMontharc();
            drawWeekarc();
            console.log('successfully get data', genData)
        },
        error: function (data) {
            console.log('data geting error', data)
        },
        dataType: "json"
    });
}

// 获取路线图数据
function getTimeData() {
    let datasite = {}
    datasite = dateTime;
    for (let i = 0; i < selectedSites.length; i += 1) {
        datasite[i] = selectedSites[i]
    }
    datasite['leng'] = selectedSites.length
    $.ajax({
        url: "http://localhost:5000/getTime",
        data: datasite,
        type: "GET",
        success: function (data) {
            Timedata = data
            drawTimeAnlysisChart();
            console.log('lxm', data)
        },
        error: function (data) {
            console.log('Timedata geting error', data)
        },
        dataType: "json"
    });
}

// 获取地图数据
function getMapData() {
    $.ajax({
        url: "http://localhost:5000/getMapData",
        data: dateTime,
        type: "GET",
        success: function (data) {
            mapData = data
            mapRefresh();
            console.log('successfully get data', mapData)
        },
        error: function (data) {
            console.log('data geting error', data)
        },
        dataType: "json"
    });
}

function MDSaFCLU() {
    let sites = {}
    for (let i = 0; i < selectedSites.length; i += 1) {
        sites[i] = selectedSites[i]
    }
    sites['leng'] = selectedSites.length
    sites['year'] = dateTime.year
    sites['month'] = dateTime.month
    sites['day'] = dateTime.day
    sites['hour'] = dateTime.hour
    sites['week'] = dateTime.week
    sites['type'] = timeAnlysisType
    sites['aqi'] = document.getElementById('aqiCheck').checked
    sites['so2'] = document.getElementById('so2Check').checked
    sites['o3_24h'] = document.getElementById('o3_24hCheck').checked
    sites['pm25'] = document.getElementById('pm25Check').checked
    sites['so2_24h'] = document.getElementById('so2_24hCheck').checked
    sites['o3_8h'] = document.getElementById('o3_8hCheck').checked
    sites['pm25_24h'] = document.getElementById('pm25_24hCheck').checked
    sites['no2'] = document.getElementById('no2Check').checked
    sites['o3_8h_24h'] = document.getElementById('o3_8h_24hCheck').checked
    sites['pm10'] = document.getElementById('pm10Check').checked
    sites['no2_24h'] = document.getElementById('no2_24hCheck').checked
    sites['co'] = document.getElementById('coCheck').checked
    sites['pm10_24h'] = document.getElementById('pm10_24hCheck').checked
    sites['o3'] = document.getElementById('o3Check').checked
    sites['co_24h'] = document.getElementById('co_24hCheck').checked
    sites['threshold'] = threshold
    // console.log(sites)
    $.ajax({
        url: "http://localhost:5000/MDSaFCLU",
        data: sites,
        type: "GET",
        success: function (data) {
            voiData = data
            console.log('successfully get MaF', voiData)
            hier()
            getLevel()
        },
        error: function (data) {
            console.log('MaF geting error', data)
        },
        dataType: "json"
    });
}

function hier() {
    let sites = {}
    for (let i = 0; i < selectedSites.length; i += 1) {
        sites[i] = selectedSites[i]
    }
    sites['leng'] = selectedSites.length
    sites['year'] = dateTime.year
    sites['month'] = dateTime.month
    sites['day'] = dateTime.day
    sites['hour'] = dateTime.hour
    sites['week'] = dateTime.week
    sites['type'] = timeAnlysisType
    sites['aqi'] = document.getElementById('aqiCheck').checked
    sites['so2'] = document.getElementById('so2Check').checked
    sites['o3_24h'] = document.getElementById('o3_24hCheck').checked
    sites['pm25'] = document.getElementById('pm25Check').checked
    sites['so2_24h'] = document.getElementById('so2_24hCheck').checked
    sites['o3_8h'] = document.getElementById('o3_8hCheck').checked
    sites['pm25_24h'] = document.getElementById('pm25_24hCheck').checked
    sites['no2'] = document.getElementById('no2Check').checked
    sites['o3_8h_24h'] = document.getElementById('o3_8h_24hCheck').checked
    sites['pm10'] = document.getElementById('pm10Check').checked
    sites['no2_24h'] = document.getElementById('no2_24hCheck').checked
    sites['co'] = document.getElementById('coCheck').checked
    sites['pm10_24h'] = document.getElementById('pm10_24hCheck').checked
    sites['o3'] = document.getElementById('o3Check').checked
    sites['co_24h'] = document.getElementById('co_24hCheck').checked
    sites['threshold'] = threshold
    console.log(sites)
    $.ajax({
        url: "http://localhost:5000/hier",
        data: sites,
        type: 'GET', //请求类型
        success: function (data) {
            hier_res = data
            console.log('successfully', hier_res)
            voronoi()
        },
        error: function (data) {
            console.log('error', data)
            voronoiClear()
        },
        dataType: "json"
    });
}

// 获取层次结构
function getLevel() {
    let sites = {}
    for (let i = 0; i < selectedSites.length; i += 1) {
        sites[i] = selectedSites[i]
    }
    sites['leng'] = selectedSites.length
    sites['year'] = dateTime.year
    sites['month'] = dateTime.month
    sites['day'] = dateTime.day
    sites['hour'] = dateTime.hour
    sites['week'] = dateTime.week
    sites['type'] = timeAnlysisType
    sites['aqi'] = document.getElementById('aqiCheck').checked
    sites['so2'] = document.getElementById('so2Check').checked
    sites['o3_24h'] = document.getElementById('o3_24hCheck').checked
    sites['pm25'] = document.getElementById('pm25Check').checked
    sites['so2_24h'] = document.getElementById('so2_24hCheck').checked
    sites['o3_8h'] = document.getElementById('o3_8hCheck').checked
    sites['pm25_24h'] = document.getElementById('pm25_24hCheck').checked
    sites['no2'] = document.getElementById('no2Check').checked
    sites['o3_8h_24h'] = document.getElementById('o3_8h_24hCheck').checked
    sites['pm10'] = document.getElementById('pm10Check').checked
    sites['no2_24h'] = document.getElementById('no2_24hCheck').checked
    sites['co'] = document.getElementById('coCheck').checked
    sites['pm10_24h'] = document.getElementById('pm10_24hCheck').checked
    sites['o3'] = document.getElementById('o3Check').checked
    sites['co_24h'] = document.getElementById('co_24hCheck').checked
    sites['threshold'] = threshold
    $.ajax({
        url: "http://localhost:5000/getTreeLevel",
        data: sites,
        type: 'GET', //请求类型
        success: function (data) {
            levels = data
            console.log('successfully', levels)
            drawTree()
        },
        error: function (data) {
            console.log('error', data)
            clearTree()
        },
        dataType: "json"
    });
}

// 时序分析作图所需部分变量
var axisType = {
    day: {
        axisAmount: 24,
        index: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
        fixed: {
            x: [1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
            y: 1
        }
    },
    week: {
        axisAmount: 7,
        index: ['Mon.', 'Tues.', 'Wed.', 'Thur.', 'Fri.', 'Sat.', 'Sun.'],
        fixed: {
            x: [3, 4, 3, 4, 3, 3, 3],
            y: 1
        }
    },
    month: {},
    year: {
        axisAmount: 12,
        index: ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'Jun.', 'Jul.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.'],
        fixed: {
            x: [3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 3],
            y: 1
        }
    }
}

// 改变 小时
function hourLabelChange() {
    let tempHour = document.getElementById('hourSelecter').value
    document.getElementById('hourLabel').value = `Hour:${tempHour}`
    dateTime.hour = tempHour
    mapRefresh();
    MDSaFCLU();
    // console.log(dateTime)
}

// 改变 阈值
function thresholdLableChange() {
    let tempThreshold = document.getElementById('thresholdSelecter').value
    document.getElementById('thresholdLabel').value = `Threshold:${tempThreshold}`
    threshold = tempThreshold;
    hier();
}

// 改变 日期
function dateChange() {
    let date = document.getElementById('dateSelecter').value
    // console.log(new Date(date).getDay())
    date = date.split('-')
    dateTime.year = date[0]
    dateTime.month = date[1]
    dateTime.day = date[2]
    dateTime.week = String(new Date(date).getDay())
    mapRefresh();
    getData();
    getTimeData()
    MDSaFCLU();
    // console.log(dateTime)
}

// 下部图表AQI颜色映射 0-500
function bottomColor(d) {
    let ShiftScale = d3.scaleLinear().domain([0, 250]).range([1, 0])
    if (d >= 0 && d <= 250) {
        return LevelColorScale2(ShiftScale(d))
    } else if (d >= 250 && d <= 500) {
        return LevelColorScale1(ShiftScale(d - 250))
    } else {
        return 'maroon'
    }
}
// 上部部图表AQI颜色映射 0-500
function topColor(d) {
    let ShiftScale = d3.scaleLinear().domain([0, 250]).range([0, 1])
    if (d >= 0 && d <= 250) {
        return LevelColorScale3(ShiftScale(d))
    } else if (d >= 250 && d <= 500) {
        return LevelColorScale4(ShiftScale(d - 250))
    } else {
        return 'maroon'
    }
}

// ================================================================================ //
// 树状图的颜色返回函数 PM2.5 500, PM10 500, SO2 500, NO2 500, CO 50, O3 500
function pm25Color(d) {
    let ShiftScale = d3.scaleLinear().domain([0, 250]).range([0, 1])
    if (d >= 0 && d <= 250) {
        return LevelColorScale3(ShiftScale(d))
    } else if (d >= 250 && d <= 500) {
        return LevelColorScale4(ShiftScale(d - 250))
    } else {
        return 'maroon'
    }
}
function pm10Color(d) {
    let ShiftScale = d3.scaleLinear().domain([0, 250]).range([0, 1])
    if (d >= 0 && d <= 250) {
        return LevelColorScale3(ShiftScale(d))
    } else if (d >= 250 && d <= 500) {
        return LevelColorScale4(ShiftScale(d - 250))
    } else {
        return 'maroon'
    }
}
function so2Color(d) {
    let ShiftScale = d3.scaleLinear().domain([0, 250]).range([0, 1])
    if (d >= 0 && d <= 250) {
        return LevelColorScale3(ShiftScale(d))
    } else if (d >= 250 && d <= 500) {
        return LevelColorScale4(ShiftScale(d - 250))
    } else {
        return 'maroon'
    }
}
function no2Color(d) {
    let ShiftScale = d3.scaleLinear().domain([0, 250]).range([0, 1])
    if (d >= 0 && d <= 250) {
        return LevelColorScale3(ShiftScale(d))
    } else if (d >= 250 && d <= 500) {
        return LevelColorScale4(ShiftScale(d - 250))
    } else {
        return 'maroon'
    }
}
function coColor(d) {
    let ShiftScale = d3.scaleLinear().domain([0, 25]).range([0, 1])
    if (d >= 0 && d <= 25) {
        return LevelColorScale3(ShiftScale(d))
    } else if (d >= 25 && d <= 50) {
        return LevelColorScale4(ShiftScale(d - 25))
    } else {
        return 'maroon'
    }
}
function o3Color(d) {
    let ShiftScale = d3.scaleLinear().domain([0, 250]).range([0, 1])
    if (d >= 0 && d <= 250) {
        return LevelColorScale3(ShiftScale(d))
    } else if (d >= 250 && d <= 500) {
        return LevelColorScale4(ShiftScale(d - 250))
    } else {
        return 'maroon'
    }
}
// ================================================================================ //

// 选择维度数改变
function checkboxChange() {
    MDSaFCLU()
}